//
//  SDNetwork.m
//  skinDetection
//
//  Created by  liangzhun on 2018/11/7.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDNetwork.h"
#import "SDSessionManager.h"
#import "SDAES+Base64.h"
#import "SDDetectionDataModel.h"
#import "SDDiagnoseSkinData.h"

@implementation SDNetwork
//任意类型转json
+ (nullable NSString *)senderToJSONWith:(nonnull id)sender{
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sender options:kNilOptions error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}
//字典转json
+ (nullable NSString * )dicToJSONWith:(nonnull NSDictionary *)dic{
    if (!dic)return nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

+ (nullable NSDictionary *)jsonStringToDic:(nonnull NSString *)jsonStr{
    
    if ([jsonStr isKindOfClass:[NSDictionary class]]) {
        return (NSDictionary *)jsonStr;
    }
    NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    return dic;
}

+ (void)postSMSLoginWithParameters:(NSDictionary *)parameters finish:(finishBlock)finish
{
    
    [[SDSessionManager shareManager].requestSerializer setValue:nil forHTTPHeaderField:@"Token"];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDPOST:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_SMSLOGIN) parameters:[self getEncryptWithDictionary:parameters] success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
       
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        responseObject = [self getDecryptWithDictionary:responseObject];
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
    
}


+ (void)postThirdLoginWithParameters:(NSDictionary *)parameters finish:(finishBlock)finish
{
    //[self getEncryptWithDictionary:parameters]
    [[SDSessionManager shareManager].requestSerializer setValue:nil forHTTPHeaderField:@"Token"];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDPOST:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_THIRDLOGIN) parameters:[self getEncryptWithDictionary:parameters] success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        responseObject = [self getDecryptWithDictionary:responseObject];
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
        
    }];
    
}


+ (void)getVerCodeWithParameters:(NSDictionary *)parameters finish:(finishBlock)finish
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@/%@/%d",BASE_URL,INTERFACE_PREFIX,SD_API_VERIFCODE,[parameters objectForKey:@"userName"],[[parameters objectForKey:@"mark"] intValue]];
    [[SDSessionManager shareManager].requestSerializer setValue:nil forHTTPHeaderField:@"Token"];
    [SDSessionManager SDGET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
       
    }];
}


+ (void)postBoundNumebrWithParameters:(NSDictionary *)parameters finish:(finishBlock)finish
{
    [SDAppDelegate addActivityView];
    [SDSessionManager SDPOST:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_BOUNDNUMBER) parameters:[self getEncryptWithDictionary:parameters] success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
    
}


+ (void)getSkinTypeDetailWithParameters:(int)parameters finish:(finishBlock)finish
{
    NSString *url = [NSString stringWithFormat:@"%@%d",SD_API_getSkinCheckSub,parameters];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, url) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}


+ (void)getProductListFinish:(finishBlock)finish
{
     [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_PRDUCTLIST) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}



+ (void)getProductCategoryListWithTypeId:(int)type Finish:(finishBlock)finish
{
    NSString *url = [NSString stringWithFormat:@"%@/%d",NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_PRDUCTCATEGORYLIST),type];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}

+ (void)getMyCheckReportListWithFinish:(finishBlock)finish
{
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_GETMYCHECKREPORT) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        responseObject = [self getDecryptWithDictionary:responseObject];
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}
+ (void)getCustCheckReportListWithFinish:(finishBlock)finish
{
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_GETCUSTCHECKREPORT) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        responseObject = [self getDecryptWithDictionary:responseObject];
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}

+ (void)getReportinfoWithReportId:(int)ID Finish:(finishBlock)finish
{
     NSString *url = [NSString stringWithFormat:@"%@%d",NSSTRINGAPPEND(INTERFACE_PREFIX,SD_API_GETREPORTINFO),ID];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        responseObject = [self getDecryptWithDictionary:responseObject];
        NSLog(@"JJJJJJJJJJJ%@",[self dicToJSONWith:responseObject]);
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
    
}

+ (void)getQuestionSurveyWithSkinTypeId:(int)skinType Finish:(finishBlock)finish
{
    NSString *url = [NSString stringWithFormat:@"%@/%d",NSSTRINGAPPEND(INTERFACE_PREFIX,SD_API_QUESTIONSURVEY),skinType];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        //responseObject = [self getDecryptWithDictionary:responseObject];
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}

+ (void)postDeleteReportWithParameter:(NSString *)parameter finish:(finishBlock)finish
{
    
    NSString *url = [NSString stringWithFormat:@"%@/%@",NSSTRINGAPPEND(INTERFACE_PREFIX,SD_API_DELETEREPORT),parameter];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDPOST:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
    
}
+ (void)getProductRecommendWithSkinSubId:(int)skinSubId ByUser:(BOOL)byUser Finish:(finishBlock)finish
{
    NSString *url = byUser? [NSString stringWithFormat:@"%@/%d/%.f",NSSTRINGAPPEND(INTERFACE_PREFIX,SD_API_RECOMMENDBYUSER),skinSubId,[SDDetectionDataModel shareInstance].userModel.id]:[NSString stringWithFormat:@"%@/%d",NSSTRINGAPPEND(INTERFACE_PREFIX,SD_API_RECOMMENDBYUSER),skinSubId];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        //responseObject = [self getDecryptWithDictionary:responseObject];
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}
+ (void)getProductRecommendAllWithSkinSubIdStr:(NSString *)skinSubIdStr ByUser:(BOOL)byUser Finish:(finishBlock)finish
{
     NSString *url = [NSString stringWithFormat:@"%@/%@",NSSTRINGAPPEND(INTERFACE_PREFIX,SD_API_RECOMMENDBYUSERALL),skinSubIdStr];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        //responseObject = [self getDecryptWithDictionary:responseObject];
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}
+ (void)postGenerateReportFinish:(finishBlock)finish
{
    int user = arc4random()%2;
    SDDetectionDataModel *modelData = [SDDetectionDataModel shareInstance];
    NSString *url = [NSString stringWithFormat:@"%@/%d",NSSTRINGAPPEND(INTERFACE_PREFIX,SD_API_GENERATEREPORT),user];
    [SDSessionManager SDPOST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
      
        for (int i = 0; i < modelData.detectionImageDic.count; i++) {
            
            SDDiagnoseSkinData *dataSkin = modelData.detectionReportDic[[NSString stringWithFormat:@"%d",i+1]];
            NSData *data = UIImagePNGRepresentation([UIImage imageNamed:modelData.detectionImageDic[[NSString stringWithFormat:@"%d",i+1]]]);
            [formData appendPartWithFileData:data name:@"imgList" fileName:[NSString stringWithFormat:@"%.f.png",dataSkin.id] mimeType:@"image/png"];
            
        }
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {

        if (error.code == -1001){
            [SDMethodTool showWarnWithContent:@"网络请求超时"];
        }
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
    
    
}
+ (void)getTurnThirdLoginFinish:(finishBlock)finish
{
    NSString *url = [NSString stringWithFormat:@"%@/%@",SD_API_ThirdLoginOff,APP_Version];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, url) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}
+ (void)getQuestionSurveyAllFinish:(finishBlock)finish
{
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_QuestionSurveyGetAll) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}

+ (void)getQuestionResultWithID:(int)resultID Answer:(NSString *)answer Finish:(finishBlock)finish
{
     NSString *url = [NSString stringWithFormat:@"%@%d/%@",SD_API_getResult,resultID,answer];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, url) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}
+ (void)postQuestionGenerateReportWithParame:(NSDictionary *)parame Finish:(finishBlock)finish
{
    [SDAppDelegate addActivityView];
    [SDSessionManager SDPOST:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_GenerateReport_Question) parameters:parame success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code == -1001){
            [SDMethodTool showWarnWithContent:@"网络请求超时"];
        }
        finish(nil , error);
        [SDAppDelegate removeActivityView];
    }];
    
    
}

+ (void)getGetAllQuestionReportWithFinish:(finishBlock)finish
{
   [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, SD_API_GetQuestionReport) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error.code == -1001){
            [SDMethodTool showWarnWithContent:@"网络请求超时"];
        }
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}

+ (void)postDeleteQuestionReportWithParameter:(NSString *)parameter finish:(finishBlock)finish
{
    
    NSString *url = [NSString stringWithFormat:@"%@/%@",NSSTRINGAPPEND(INTERFACE_PREFIX,SD_API_DeleteQuestionReport),parameter];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDPOST:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error.code == -1001){
            [SDMethodTool showWarnWithContent:@"网络请求超时"];
        }
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
    
}

+ (void)getQuestionReportDetailWithID:(int)reportID Finish:(finishBlock)finish
{
    NSString *url = [NSString stringWithFormat:@"%@%d",SD_API_GetQuestionReportDetail,reportID];
    [SDAppDelegate addActivityView];
    [SDSessionManager SDGET:NSSTRINGAPPEND(INTERFACE_PREFIX, url) parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
       [SDAppDelegate removeActivityView];
        if ([responseObject[@"status"] intValue] != 200) {
            [self logErrorWithCode:[responseObject[@"status"] intValue]];
            return ;
        }
        
        finish(responseObject , nil);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        finish(nil , error);
        [SDAppDelegate removeActivityView];
        
    }];
}

#pragma mark - 返回错误码
+ (void)logErrorWithCode:(int)code
{
    code == 506?  [SDMethodTool alertWithContent:SERVICE_ERR(code) handler:^(UIAlertAction * _Nonnull action) {
        
        [SDAppDelegate goLogin];
        
    }]:[SDMethodTool alertWithContent:SERVICE_ERR(code) handler:nil];
    
    
}
#pragma mark - 加密模块
+ (id)getEncryptWithDictionary:(NSDictionary *)dic
{
    return [SDAES_Base64 encryptAES:[self dicToJSONWith:dic]];
}
+ (id)getDecryptWithDictionary:(NSDictionary *)dic
{
    
    if (!dic[@"data"]) {
        return dic;
    }
    
    return [self jsonStringToDic:[SDAES_Base64 decryptAES:dic[@"data"]]];
    
}
@end

